import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	Deck deck;
	Hand[] hand;
	int turn = 0, input;

	public Main() throws IOException {
		setup();
		while (1 != 2) {
			System.out.println("It is Player " + (turn + 1)
					+ "'s turn. What would you like to do?");
			System.out.println("(1) Draw (2) Pass Cards (3) Check Winner");
			input = Integer.parseInt(in.readLine());
			if (input == 1) {
				hand[turn].drawCard(deck);
			} else if (input == 2) {
				System.out.println("Which cards would you like to pass on?");
				String[] cards = new String[2];
				try {
					cards = in.readLine().split(" ");
				} catch (IOException e) {
					e.printStackTrace();
				}
				int first = 0, second = 0;
				for (int i = 0; i < hand[turn].getHand().size(); i++) {
					String temp = hand[turn].getHand().get(i).trim();
					if (temp.equalsIgnoreCase(cards[0])) {
						first = i;
					} else if (temp.equalsIgnoreCase(cards[1])) {
						if (i != 0){
							second = i - 1;
						} else {
							second = i;
						}
						
					}
				}
				hand[turn].passCards(first, second, hand, cards, turn);
			} else if (input == 3) {
				System.out.println("The winner is....");
				System.out.println(checkWinner());
				break;
			}
			if (turn == 3) {
				turn = 0;
			} else {
				turn++;
			}
			for (int i = 0; i < 4; i++) {
				hand[i].displayHand(i + 1);
			}
		}
	}

	private String checkWinner() {
		int winner = 0;
		for (int i = 1; i < 4; i++) {
			if (hand[i].getScore() > hand[winner].getScore()) {
				winner = i;
			}
		}
		return new String("Player " + ++winner);
	}

	private void setup() {
		deck = new Deck();
		hand = new Hand[4];
		for (int i = 0; i < 4; i++) {
			hand[i] = new Hand(deck);
		}
		for (int i = 0; i < 4; i++) {
			hand[i].displayHand(i + 1);
		}
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		new Main();
	}

}
